/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cmti.skeleton.service.impl;

import com.cmti.commons.util.StringUtils;
import com.cmti.skeleton.dao.mapper.SysRightsMapper;
import com.cmti.skeleton.dao.mapper.SysRoleMapper;
import com.cmti.skeleton.dao.mapper.SysUserMapper;
import com.cmti.skeleton.dto.SysLogDto;
import com.cmti.skeleton.dto.SysRoleDto;
import com.cmti.skeleton.dto.TreeNode;
import com.cmti.skeleton.entity.SysLog;
import com.cmti.skeleton.entity.SysRights;
import com.cmti.skeleton.entity.SysRole;
import com.cmti.skeleton.service.RoleService;
import com.cmti.skeleton.service.SysLogService;
import com.cmti.skeleton.web.shiro.ShiroRealm;
import com.github.pagehelper.PageHelper;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Danny Lee CMTI
 */
@Service
@Transactional(readOnly = true)
public class RoleServiceImpl implements RoleService {

    private final Log log = LogFactory.getLog(this.getClass());
    @Autowired
    SysRoleMapper roleMapper;
    @Autowired
    SysRightsMapper rightMapper;
    @Autowired
    SysUserMapper userMapper;
    @Autowired
    SysLogService sysLogService;

    @Override
    public List<SysRoleDto> getRoles(SysRoleDto role) {
        PageHelper.startPage(role.getPage(), role.getRows());
        return roleMapper.selectListByDto(role);
    }

    @Override
    public List<TreeNode> getRightsTreeByRoleId(long roleId) {
        //取一级权限
        List<SysRights> rl = rightMapper.selectByRoleIdAndParentId(roleId, 0);
        List<TreeNode> rightTree = new LinkedList<TreeNode>();
        for (SysRights sr : rl) {
            TreeNode tn = new TreeNode(sr.getId(), sr.getName(), false, null);
            rightTree.add(tn);

            genSubRights(roleId, tn);
        }

        return rightTree;
    }

    private void genSubRights(long roleId, TreeNode tn) {
        List<SysRights> rl = rightMapper.selectByRoleIdAndParentId(roleId, tn.getId());
        List<TreeNode> children = new LinkedList<TreeNode>();
        tn.setChildren(children);
        for (SysRights sr : rl) {
            TreeNode treeNode = new TreeNode(sr.getId(), sr.getName(), false, null);
            children.add(treeNode);

            genSubRights(roleId, treeNode);
        }
    }

    @Override
    public List<SysRole> queryRoleList(Long userId) {
        return roleMapper.selectByUserId(userId);
    }

    @Transactional
    @Override
    public void addRole(SysRoleDto roleDto) {
        roleMapper.insertSelective(roleDto);
        // 添加权
        addRights(roleDto);
    }

    private void addRights(SysRoleDto roleDto) throws NumberFormatException {
        long roleId = roleDto.getId();
        String[] rightIds = null;
        if (StringUtils.isNotNull(roleDto.getRightIds())) {
            rightIds = roleDto.getRightIds().split(",");
        }
        if (rightIds != null) {
            for (String rightId : rightIds) {
                rightMapper.insertRoleRight(roleId, Long.valueOf(rightId));
            }
        }
    }

    @Transactional
    @Override
    public int deleteRole(long roleId) {
//        验证是否有关联的用户,如果有,不允许删除
        if (userMapper.countByRoleId(roleId) > 0) {
            return 1;
        } else {
            rightMapper.deleteRightsByRoleId(roleId);
            roleMapper.deleteByPrimaryKey(roleId);
            return 0;
        }
    }

    @Override
    public List<SysRights> getRightsByRoleId(long roleId) {
        return rightMapper.selectByRoleId(roleId);
    }

    @Transactional
    @Override
    public void modifyRole(SysRoleDto roleDto) {
        roleMapper.updateByPrimaryKeySelective(roleDto);
        // 更新权限
        long roleId = roleDto.getId();
        // 先删除
        rightMapper.deleteRightsByRoleId(roleId);
        // 再添加
        addRights(roleDto);
    }

    @Override
    @Transactional
    public void lockOrUnlock(long roleId, int status) {
        SysRole role = new SysRole();
        role.setId(roleId);
        role.setStatus(status);
        roleMapper.updateByPrimaryKeySelective(role);
    }

    @Override
    public SysRole getRoleById(long roleId) {
        return roleMapper.selectById(roleId);
    }

    @Override
    public List<SysRights> getRightsByUserId(long uid) {
        return rightMapper.selectByUserId(uid);
    }
}
